package net.ojbk.study.curriculumDesign.web.handlers.cilentservicer.impl;

import net.ojbk.study.curriculumDesign.beans.cilentservicer.*;
import net.ojbk.study.curriculumDesign.beans.usermanage.User;
import net.ojbk.study.curriculumDesign.service.cilentservicer.ICenterCommService;
import net.ojbk.study.curriculumDesign.service.cilentservicer.ICenterManaService;
import net.ojbk.study.curriculumDesign.web.handlers.cilentservicer.ICenterController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: twobox
 * Date: 2019/1/9
 * Time: 14:22
 * Description: No Description
 */
@Controller
@RequestMapping("/cilentservicer")
public class CenterControllerImpl implements ICenterController {
    @Autowired
    @Qualifier("centerManaService")
    ICenterManaService centerManaService;

    @Autowired
    @Qualifier("centerCommService")
    ICenterCommService centerCommService;

    @Autowired
    @Qualifier("chatSystem")
    ChatSystem chatSystem;


    /**
     * 获取正在通话的Chat
     *
     * @param session
     * @return
     */
    @RequestMapping(value = "chatingList", method = RequestMethod.GET)
    public ResponseEntity<Object> getChatingList(HttpSession session) {
        try {
            if (session.getAttribute("servicer") == null)
                throw new Exception("你不是客服，没有权限");

            List<RequestChat> ReturnChatingList = new ArrayList<>();
            RequestChat tempChat = null;

            for (Chat chat : centerCommService.getAllChating()) {
                tempChat = RequestChat.getChating(chat);
                ReturnChatingList.add(tempChat);
            }
            return new ResponseEntity<Object>(ReturnChatingList, HttpStatus.OK);
        } catch (Exception e) {;
            return new ResponseEntity<Object>(e.getMessage(), HttpStatus.BAD_REQUEST);
        }

    }


    // 一上未定义接口

    /**
     * 成为客服
     *
     * @param httpSession
     * @return
     */
    @Override
    @RequestMapping(value = "servicesList", method = RequestMethod.POST)
    public ResponseEntity<Object> becomeServicer(HttpSession httpSession) {
        User user = (User) httpSession.getAttribute("user");
        try {
            Servicer servicer = centerManaService.becomeServicer(user);
            httpSession.setAttribute("servicer", servicer);
            return new ResponseEntity<Object>("欢迎加入客服中心！", HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<Object>(new String(e.getMessage()), HttpStatus.BAD_REQUEST);
        }
    }

    /**
     * 逃离中心
     *
     * @param httpSession
     * @return
     */
    @Override
    @RequestMapping(value = "servicesList", method = RequestMethod.DELETE)
    public ResponseEntity<Object> escapeCenter(HttpSession httpSession) {
        Servicer servicer = (Servicer) httpSession.getAttribute("servicer");
        try {
            centerManaService.escapeCenter(servicer);
            httpSession.setAttribute("servicer", null);
            return new ResponseEntity<Object>("逃离客服中心成功！", HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<Object>(new String(e.getMessage()), HttpStatus.BAD_REQUEST);
        }
    }

    /**
     * 获取所有客服
     *
     * @param httpSession
     * @return
     */
    @Override
    @RequestMapping(value = "servicesList", method = RequestMethod.GET)
    public ResponseEntity<Object> getAllServicer(HttpSession httpSession) {
        List<Servicer> allServicers = centerManaService.getAllServicers();
        return new ResponseEntity<Object>(allServicers, HttpStatus.OK);
    }

    /**
     * 查看是否有新的请求
     *
     * @param httpSession 需要验证此session是否为客服
     * @return
     */
    @Override
    @RequestMapping(value = "haveNewRequestList", method = RequestMethod.GET)
    public ResponseEntity<Object> isHaveNewRequest(HttpSession httpSession) {
        if (httpSession.getAttribute("servicer") == null)
            return new ResponseEntity<Object>("你不是客服，没有权限", HttpStatus.BAD_REQUEST);

        return new ResponseEntity<Object>(centerCommService.isHaveNewRequest(), HttpStatus.OK);
    }

    /**
     * 获取所有待接受的请求
     *
     * @param httpSession 需要验证此httpSession是否为客服
     * @return
     */
    @Override
    @RequestMapping(value = "newRequestList", method = RequestMethod.GET)
    public ResponseEntity<Object> getAllRequest(HttpSession httpSession) {
        try {
            if (httpSession.getAttribute("servicer") == null)
                throw new Exception("你不是客服，没有权限");
            List<RequestChat> requestChatList = new ArrayList<>();
            RequestChat tempChat = null;
            List<Chat> allRequestChat = centerCommService.getAllRequestChat();
            for (Chat chat : allRequestChat) {
                tempChat = RequestChat.getRequestChat(chat);
                requestChatList.add(tempChat);
            }
            return new ResponseEntity<Object>(requestChatList, HttpStatus.OK);
        } catch (Exception e) {;
            return new ResponseEntity<Object>(e.getMessage(), HttpStatus.BAD_REQUEST);
        }

    }

    /**
     * 获取该用户的 实时聊天记录
     *
     * @param httpSession
     * @param uid         用户id
     * @return
     */
    @Override
    @RequestMapping(value = "chatMsg", method = RequestMethod.GET)
    public ResponseEntity<Object> getChatMsg(HttpSession httpSession, String uid) {
        try {
            if (httpSession.getAttribute("servicer") == null)
                throw new Exception("没有权限");
            Chat chat = chatSystem.getChatingByUser(new User(uid));
            List<ChatMessage> chatAllMsg = centerCommService.getChatAllMsg(chat);
            return new ResponseEntity<Object>(chatAllMsg, HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<Object>(new String(e.getMessage()), HttpStatus.BAD_REQUEST);
        }
    }

}
